import Vue from 'vue'
import Router from 'vue-router'
import App from '../App'
import * as types from '../store/types'
import store from '../store/store'


const index = r => require.ensure([], () => r(require('../page/official_site/index.vue')), 'index')
const news = r => require.ensure([], () => r(require('../page/official_site/news.vue')), 'news')
const article = r => require.ensure([], () => r(require('../page/official_site/article.vue')), 'article')
const contact = r => require.ensure([], () => r(require('../page/official_site/contact.vue')), 'contact')
const more = r => require.ensure([], () => r(require('../page/official_site/more.vue')), 'more')
const about = r => require.ensure([], () => r(require('../page/official_site/about.vue')), 'about')
const cases = r => require.ensure([], () => r(require('../page/official_site/case.vue')), 'case')
const product = r => require.ensure([], () => r(require('../page/official_site/product.vue')), 'product')
const solution = r => require.ensure([], () => r(require('../page/official_site/solution.vue')), 'solution')
const detail = r => require.ensure([], () => r(require('../page/official_site/detail.vue')), 'detail')
const myuser = r => require.ensure([], () => r(require('../page/official_site/myuser.vue')), 'myuser')
const cashinfo = r => require.ensure([], () => r(require('../page/official_site/cashinfo.vue')), 'cashinfo')
const login = r => require.ensure([], () => r(require('../page/official_site/login.vue')), 'login')
const register = r => require.ensure([], () => r(require('../page/official_site/register.vue')), 'register')
const updatauser = r => require.ensure([], () => r(require('../page/official_site/updatauser.vue')), 'updatauser')
const uList = r => require.ensure([], () => r(require('../page/official_site/uList.vue')), 'uList')
const sList = r => require.ensure([], () => r(require('../page/official_site/sList.vue')), 'sList')
const auditlist = r => require.ensure([], () => r(require('../page/official_site/auditlist.vue')), 'auditlist')
const auditorder = r => require.ensure([], () => r(require('../page/official_site/auditorder.vue')), 'auditorder')
const addtask = r => require.ensure([], () => r(require('../page/official_site/addtask.vue')), 'addtask')
const subtask = r => require.ensure([], () => r(require('../page/official_site/subtask.vue')), 'subtask')
const toBeVip = r => require.ensure([], () => r(require('../page/official_site/toBeVip.vue')), 'toBeVip')
const tosavemoney = r => require.ensure([], () => r(require('../page/official_site/tosavemoney.vue')), 'tosavemoney')


Vue.use(Router)
const routes = [
  {
    path: '/',
    redirect: '/index',
    component: App,
    children: [
      {
        path: '/product',
        component: product
      },
      {
        path: '/solution',
        component: solution
      }, {
        path: '/index',
        component: index
      }, {
        path: '/news',
        component: news
      }, {
        path: '/article',
        component: article
      }, {
        path: '/contact',
        component: contact
      }, {
        path: '/more',
        component: more
      },
      {
        path: '/about',
        component: about
      },
      {
        path: '/case',
        component: cases
      },{
        path: '/login',
        component: login
      },{
        path: '/detail',
        component: detail,
        meta: { title: '详情' , requireAuth: true }
      },{
        path: '/addtask',
        component: addtask,
        meta: { title: '发布任务' , requireAuth: true }
      },{
        path: '/subtask',
        component: subtask,
        meta: { title: '发布任务' , requireAuth: true }
      },{
        path: '/auditlist',
        component: auditlist,
        meta: { title: '订单审核列表' , requireAuth: true }
      },{
        path: '/myuser',
        component: myuser
      },{
        path: '/cashinfo',
        component: cashinfo,
        // meta: { title: '明细' , requireAuth: true }
      },{
        path: '/updatauser',
        component: updatauser,
        meta: { title: '修改用户信息' , requireAuth: true }
      },{
        path: '/register',
        component: register,
      },{
        path: '/uList',
        component: uList,
        meta: { title: '我的订单' , requireAuth: true }
      },{
        path: '/sList',
        component: sList,
        meta: { title: '我的任务' , requireAuth: true }
      },{
        path: '/auditorder',
        component: auditorder,
        meta: { title: '审核订单' , requireAuth: true }
      }, {
        path: '/toBeVip',
        component: toBeVip,
        meta: { title: '充值vip', requireAuth: true }
      }, {
        path: '/tosavemoney',
        component: tosavemoney,
        meta: { title: '充值', requireAuth: true }
      }
    ]
  }
]

if (window.localStorage.getItem('token')) {
  var data = {
    token: localStorage.getItem('token'),
    account: localStorage.getItem('account')
  }
  store.commit(types.LOGIN, data)
}

const router = new Router({
  routes
})

const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

router.afterEach((to, from) => {
  reloadInterceptor(to, from)
})
const reloadInterceptor = (to, from) => {
  if ((from.path != '/collectProtocol' && from.path != '/prepareBindCard') && to.path == '/bankCard') {
    let isRefresh = sessionStorage.getItem('isRefresh')
    if (isRefresh === '0') {
      sessionStorage.setItem('isRefresh', null)
      window.location.reload()
    } else {
      sessionStorage.setItem('isRefresh', '0')
    }
  }
}
router.beforeEach((to, from, next) => {
  console.log("to")
  console.log(to)
  console.log("from")
  console.log(from)
  if (to.matched.some(r => r.meta.requireAuth)) {
    console.log("走if token")
    if (store.state.token) {
      next()
    } else {
      console.log("referrer",window.localStorage.getItem('referrer'))
      if(to.fullPath == '/register' || to.fullPath.indexOf('/register') != -1){
        window.localStorage.setItem('referrer', '/index')
      }else{
        window.localStorage.setItem('referrer', to.fullPath)
      }
      console.log("referrer",window.localStorage.getItem('referrer'))
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
})

export default router
